<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * @name Structure_management
 * @file structure_management.php
 * @author Daniel Valkovič
 * @version 1.0
 * @package CodeIgniter
 * @subpackage Controllers
 * @section POPIS
 * Tento kontroler slúži na úpravu štruktúry zadelenia programov,
 * ich rozdelenie do skupín a jednoznačnú identifikáciu. Tieto kontrolery 
 * sú defaultne prístupné iba pre admina.
 */
class Structure_managment extends MY_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model(array('worktypemodel', 'languagemodel', 'programosmodel', 'studylevelmodel', 'classroomsubjectmodel'));  // používanie Modelov z Acicrud
    }

    /**
     * Funkcia zobrazí základnú stránku manažmentu štruktúry.
     */
    public function index() {
        $data['title'] = 'Manažment štruktúry';
        if (!$this->user->isLogged()) {
            $data['error_message'] = 'Nie ste prihlásený!';
            $data['redirect_to'] = ''; // url , kde sa bude redirectovat '' home, inac napr 'programs_managment/index' inde
            $this->load->view('templates/headers/h_redirect_index', $data, FALSE);
            $this->load->view('auth/log_message', $data, FALSE);
            $this->load->view('templates/footers/footer', null, FALSE);
            return;
        }
        $data['work_types'] = $this->worktypemodel->getAll();
        $data['languages'] = $this->languagemodel->getAll();
        $data['program_oses'] = $this->programosmodel->getAll();
        $data['study_levels'] = $this->studylevelmodel->getAll();
        $data['classroom_subjects'] = $this->classroomsubjectmodel->getAll();
        $this->load->view('templates/headers/header', $data, FALSE);
        $this->load->view('structure_managment/index', $data, FALSE);
        $this->load->view('templates/footers/footer', null, FALSE);
    }

    /**
     * Pomocná funkcia, ktorá doplňuje názov stránky na základe posielaného parametru.
     * @param type $cat 
     * @return string
     */
    private function create_subtitle($cat) {
        switch ($cat) {
            case 'worktype':
                return 'typ práce';
            case 'language':
                return 'jazyk';
            case 'programos':
                return 'OS';
            case 'studylevel':
                return 'ročník';
            case 'classroomsubject':
                return 'predmet';
            default:
                return '';
        }
    }

    /**
     * Funkcia slúži na pridávanie novej položky do požadovanej kategórie.
     * 
     * @param type $cat
     */
    public function add_category($cat) {
        $data['title'] = 'Manažment štruktúry';
        if (!$this->user->isLogged()) {
            $data['error_message'] = 'Nie ste prihlásený!';
            $data['redirect_to'] = ''; // url , kde sa bude redirectovat '' home, inac napr 'programs_managment/index' inde
            $this->load->view('templates/headers/h_redirect_index', $data, FALSE);
            $this->load->view('auth/log_message', $data, FALSE);
            $this->load->view('templates/footers/footer', null, FALSE);
            return;
        }
        $this->load->library('form_validation');
        $this->form_validation->set_rules('name', 'Nazov', 'required|max_length[45]');
        $view = 'structure_managment/add_category';
        $sub = $this->create_subtitle($cat);
        if ($sub == '') {
            $data['error_message'] = 'Pokus o pridanie do neexistujúcej kategórie!';
            $view = 'structure_managment/category_message';
        } else {
            $data['what_add'] = 'Pridať nový ' . $sub;
            $data['title'] = 'Manažment štruktúry - ' . $data['what_add'];
            $data['category'] = $cat;
            if ($_SERVER['REQUEST_METHOD'] == 'POST') {
                if ($this->form_validation->run() == FALSE) {
                    if ($this->input->post('name') == '')
                        $data['error_message'] = 'Položka "Názov" je povinná!';
                    else
                        $data['error_message'] = 'Položka "Názov" je pridlhá! Maximum je 45 znakov';
                } else {
                    //Validation success
                    $o->name = $this->input->post('name');
                    switch ($cat) {
                        case 'worktype':
                            $this->worktypemodel->create($o);
                            break;
                        case 'language':
                            $this->languagemodel->create($o);
                            break;
                        case 'programos':
                            $this->programosmodel->create($o);
                            break;
                        case 'studylevel':
                            $this->studylevelmodel->create($o);
                            break;
                        case 'classroomsubject':
                            $this->classroomsubjectmodel->create($o);
                    }
                    $data['accept_message'] = 'Úspešne pridané';
                }
            }
            $data['name'] = array('name' => 'name',
                'id' => 'name',
                'type' => 'text',
                'placeholder' => 'Názov',
                'value' => $this->form_validation->set_value('name'),
            );
        }
        $this->load->view('templates/headers/header', $data, FALSE);
        $this->load->view($view, $data, FALSE);
        $this->load->view('templates/footers/footer', null, FALSE);
    }

    /**
     * Funkcia slúži na editovanie určitej položky kategórie.
     * 
     * @param type $cat
     * @param type $id
     */
    public function edit_category($cat, $id) {
        $data['title'] = 'Manažment štruktúry';
        if (!$this->user->isLogged()) {
            $data['error_message'] = 'Nie ste prihlásený!';
            $data['redirect_to'] = ''; // url , kde sa bude redirectovat '' home, inac napr 'programs_managment/index' inde
            $this->load->view('templates/headers/h_redirect_index', $data, FALSE);
            $this->load->view('auth/log_message', $data, FALSE);
            $this->load->view('templates/footers/footer', null, FALSE);
            return;
        }
        $this->load->library('form_validation');
        $this->form_validation->set_rules('name', 'Nazov', 'required|max_length[45]');
        $view = 'structure_managment/edit_category';
        if ($this->create_subtitle($cat) == '') {
            $data['error_message'] = 'Pokus o úpravu položky v neexistujúcej kategórii!';
            $view = 'structure_managment/category_message';
        } else {
            switch ($cat) {
                case 'worktype':
                    if ($this->worktypemodel->idExists($id))
                        $old = $this->worktypemodel->read(array('id' => $id));
                    break;
                case 'language':
                    if ($this->languagemodel->idExists($id))
                        $old = $this->languagemodel->read(array('id' => $id));
                    break;
                case 'programos':
                    if ($this->programosmodel->idExists($id))
                        $old = $this->programosmodel->read(array('id' => $id));
                    break;
                case 'studylevel':
                    if ($this->studylevelmodel->idExists($id))
                        $old = $this->studylevelmodel->read(array('id' => $id));
                    break;
                case 'classroomsubject':
                    if ($this->classroomsubjectmodel->idExists($id))
                        $old = $this->classroomsubjectmodel->read(array('id' => $id));
            }
            if (isset($old)) {
                $data['what_edit'] = 'Upraviť položku ' . $old->name . ' v kategórii ' . $this->create_subtitle($cat);
                $data['title'] = 'Manažment štruktúry - ' . $data['what_edit'];
                $data['category'] = $cat;
                $data['id_item'] = $id;
                if ($_SERVER['REQUEST_METHOD'] == 'POST') {
                    if ($this->form_validation->run() == FALSE) {
                        if ($this->input->post('name') == '')
                            $data['error_message'] = 'Položka "Názov" je povinná!';
                        else
                            $data['error_message'] = 'Položka "Názov" je pridlhá! Maximum je 45 znakov';
                    } else {
                        //Validation success
                        $o->id = $id;
                        $o->name = $this->input->post('name');
                        switch ($cat) {
                            case 'worktype':
                                $this->worktypemodel->update($o);
                                break;
                            case 'language':
                                $this->languagemodel->update($o);
                                break;
                            case 'programos':
                                $this->programosmodel->update($o);
                                break;
                            case 'studylevel':
                                $this->studylevelmodel->update($o);
                                break;
                            case 'classroomsubject':
                                $this->classroomsubjectmodel->update($o);
                        }
                        $data['accept_message'] = 'Úspešne upravené z ' . $old->name . ' na ' . $this->input->post('name');
                    }
                }
                $data['name'] = array('name' => 'name',
                    'id' => 'name',
                    'type' => 'text',
                    'placeholder' => $old->name,
                    'value' => $this->form_validation->set_value('name'),
                );
            } else {
                $data['error_message'] = 'Pokus o upravenie neexistujúcej položky!';
                $view = 'structure_managment/category_message';
            }
        }
        $this->load->view('templates/headers/header', $data, FALSE);
        $this->load->view($view, $data, FALSE);
        $this->load->view('templates/footers/footer', null, FALSE);
    }

}

/* End of file structure_managment.php */
/* Location: ./application/controllers/structure_managment.php */
